package com.yh.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.annotation.SystemServiceLog;
import com.yh.bean.CountUserByDept;
import com.yh.bean.Dept;
import com.yh.bean.Role;
import com.yh.bean.User;
import com.yh.dao.UserDao;
import com.yh.service.UserService;

@Service //注解方式注入service 
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Override
	@SystemServiceLog(description = "查询所有员工信息")
	public Map<String, Object> getUserInfoByIf(Map<String, Object> map) {
		//1.通过map中的currentpage 获前台当前页
		int pageNum =(int) map.get("currentPage");
		//2.使用mybatis pageHelper 来进行分页查找所有的list 集合 注意一定要写到查询之前
		PageHelper.startPage(pageNum, 2);//返回值是一个page 对象，可以不使用
		
		//3.调用dao 层方法获取数据库内容 ，使用注解依赖注入的方式
		List<User> userList=userDao.getUserListByIf(map);
		
		//4.调用pageInfo 对象获取分页信息，在这里我们需要totalpages
		PageInfo<User> info = new PageInfo<User>(userList);
		int totalPages=info.getPages();
		
		//5.创建一个新的map 数据添加进去并返回
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("userList", userList);
		map1.put("totalPages", totalPages);
	
		return map1;
	}

	@Override
	public Map<String, Object> getDeptAndRole() {
		//1.调用dao层方法获取dept
		List<Dept> dList = userDao.getDeptList();
		//2.调用dao层方法获取role
		List<Role> rList = userDao.getRoleList();
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("dList", dList);
		map.put("rList", rList);
		
		return map;
	}

	@Override
	@SystemServiceLog(description = "新增一条员工信息")
	public void insertUserInfo(User user) {
		//1.调用dao层对象进行插入
		int result =userDao.insertUser(user);
		if(result != 0){
			System.out.println("=======你新增的员工编号是："+user.getUserid());
		}
		
	}

	@Override
	public Map<String, Object> getDeptAndRoleAndUserById(int userid) {
		        //1.调用dao层方法获取dept
				List<Dept> dList = userDao.getDeptList();
				//2.调用dao层方法获取role
				List<Role> rList = userDao.getRoleList();
				//3.调用dao层方法获取User
				User user = userDao.getUserById(userid);
				
				Map<String, Object> map = new HashMap<String, Object>();
				
				map.put("dList", dList);
				map.put("rList", rList);
				map.put("user", user);
				
				return map;
	}

	@Override
	@SystemServiceLog(description = "修改一条员工信息")
	public void updateUserInfo(User user) {
		//1.调用dao层对象更新数据
		int result =userDao.updateUser(user);
		if(result !=0){
			System.out.println("用户信息已更新成功！！！");
		}
		
	}

	@Override
	@SystemServiceLog(description = "删除一条员工信息")
	public String deleteUserInfoById(int userid) {
		//1.调用dao层对象更新数据
				int result =userDao.deleteUserById(userid);
				if(result !=0){
					return "ok";
				}else{
					return "no";
				}
	}

	@Override
	public User findUserByUserName(String username) {
		//1.调用dao层方法进行查找
		
		return userDao.getUserByUserName(username);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> countUserandDept() {
		//1.调用dao层方法查找所有部门名称
		List<Dept> depts = userDao.getDeptList();
		Set<String> deptNames = new HashSet<String>();
		for (Dept dept : depts) {
			
			deptNames.add(dept.getDept_name()); 
		}
		//2.调用dao层方法查找各个部门名称对应的人数的实体类对象
		List<CountUserByDept> cpt = userDao.getCountUserandDeptName();
		
		Map<String, Object> map1 = new HashMap<String, Object>();
		
		map1.put("legend", deptNames);
		map1.put("series", cpt);
		
		return map1;
	}

}
